package com.nidu.demo.company.executor.command;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.company.ability.CompanyDomainService;
import com.nidu.demo.company.convertor.CompanyConvertor;
import com.nidu.demo.company.dto.CompanyCO;
import com.nidu.demo.company.dto.CompanyUpdateCmd;
import com.nidu.demo.company.gateway.CompanyGateway;
import com.nidu.demo.company.model.Company;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 公司更新命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class CompanyUpdateCmdExe {

    private final CompanyGateway gateway;

    private final CompanyDomainService domainService;

    private final CompanyConvertor convertor;

    public SingleResponse<CompanyCO> execute(CompanyUpdateCmd cmd) {
        // 1. 转换为领域对象
        Company company = Company.update(cmd.getId(), cmd.getCompanyName(), cmd.getCompanyAddress(), cmd.getCompanyPhoneNumber(), cmd.getSortOrder());

        // 2. 检查对象是否存在
        domainService.validateDuplicate(company.getCompanyName(), company.getId());

        // 3. 调用网关保存
        Company save = gateway.update(company);

        // 4. 转换为DTO返回
        CompanyCO companyCO = convertor.toClientObject(save);

        return SingleResponse.of(companyCO);
    }

    public Response updateStatus(Long id, StatusEnum status) {
        gateway.updateStatus(id, status.getValue());
        return Response.buildSuccess();
    }
} 